package com.permission.controller;

import com.permission.model.Role;
import com.permission.service.RoleService;
import com.permission.service.impl.RoleServiceImpl;
import com.permission.util.WebUtil;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        pathInfo = pathInfo == null ? "" : pathInfo;

        switch (pathInfo) {
            case "/search":
                searchRole(request, response);
                break;
            case "/searchPage":
                showSearchPage(request, response);
                break;
            case "/menus":
                showRoleMenusPage(request, response);
                break;
            case "/saveMenus":
                saveRoleMenus(request, response);
                break;
            default:
                WebUtil.redirect(request, response, "/index.jsp");
        }
    }

    // 显示角色查询页面
    private void showSearchPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/role/searchRole.jsp").forward(request, response);
    }

    // 查询角色及其菜单
    private void searchRole(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long roleId = WebUtil.getLongParameter(request, "roleId");
        String roleKey = WebUtil.getParameter(request, "roleKey");

        Role role = null;
        if (roleId != null) {
            role = roleService.getRoleWithMenusById(roleId);
        } else if (!roleKey.isEmpty()) {
            role = roleService.getRoleWithMenusByKey(roleKey);
        }

        request.setAttribute("role", role);
        request.getRequestDispatcher("/WEB-INF/jsp/role/roleDetail.jsp").forward(request, response);
    }

    // 显示角色权限设置页面
    private void showRoleMenusPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long roleId = WebUtil.getLongParameter(request, "roleId");
        if (roleId == null) {
            WebUtil.redirect(request, response, "/role/searchPage");
            return;
        }

        Role role = roleService.getRoleWithMenusById(roleId);
        request.setAttribute("role", role);
        // 这里应该获取所有菜单列表，用于展示可选菜单
        // request.setAttribute("allMenus", menuService.getAllMenus());

        request.getRequestDispatcher("/WEB-INF/jsp/role/roleMenus.jsp").forward(request, response);
    }

    // 保存角色权限设置
    private void saveRoleMenus(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Long roleId = WebUtil.getLongParameter(request, "roleId");
        String[] menuIdsStr = request.getParameterValues("menuIds");

        if (roleId != null && menuIdsStr != null && menuIdsStr.length > 0) {
            List<Long> menuIds = Arrays.stream(menuIdsStr)
                    .map(Long::parseLong)
                    .collect(java.util.stream.Collectors.toList());

            roleService.updateRoleMenus(roleId, menuIds);
        }

        WebUtil.redirect(request, response, "/role/searchPage");
    }
}